<template>
  <div class="past">
    <button  type="button" class="el-button el-button--text el-button--small" v-if="!disabled" style="float: left; padding: 3px 0px;" @click="handleadd(0,'')"><!----><!----><span>添加既往史</span></button>
    <el-dialog
      title="添加既往史"
      :visible.sync="dialogFormVisible"
      width="500px"
      @close="closeDialog"
    >
      <el-form ref="form" :model="form" label-width="120px"  :rules="rules">
        <el-form-item label="病种" prop="diseases">
          <el-select v-model="form.diseases"   placeholder="病种"  >
            <el-option
              v-for="item in diseasess"
              :key="item.dcNo"
              :label="item.name"
              :value="item.dcNo">
            </el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="确诊时间" prop="diagnosisTime" >
          <el-date-picker
            v-model="form.diagnosisTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否接受治疗" prop="isAcceptTreat" >
          <el-select v-model="form.isAcceptTreat"   placeholder="是否接受治疗"  >
            <el-option
              v-for="item in isAcceptTreats"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary"  @click="Saveadd">确 定</el-button>
      </div>


    </el-dialog>

    <el-table
      :data="list"
      width="100%"

    >

      <el-table-column
        show-overflow-tooltip
        label="老人编号"
        prop="elderlyCode"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="diseasesName"
        label="疾病类型"

      ></el-table-column>




      <el-table-column  label="确诊时间">
        <template slot-scope="scope">
          {{scope.row.diagnosisTime | parseTime('{y}-{m}-{d}')}}
        </template>
      </el-table-column>
      <el-table-column label="是否接受治疗" prop="gender">
        <template slot-scope="scope">
                <span v-if="scope.row.acceptTreat==true">

             <el-tag type="success">是</el-tag>
                </span>
          <span v-if="scope.row.acceptTreat==false">     <el-tag type="warning">否</el-tag></span>
        </template>
      </el-table-column>

      <el-table-column label="操作" prop="disabled" v-if="!disabled">
        <template slot-scope="scope">
          <el-button type="text"  @click="doDelete(scope.$index, scope.row)"  show-overflow-tooltip width="200px">删除<i class="el-icon-delete"></i></el-button>

        </template>


      </el-table-column>
    </el-table>
  </div>
</template>

<script>
    import { search } from "@/api/past"
    import { add } from "@/api/past"
    import { parseTime} from "@/utils/index";
    import { del } from "@/api/past"
    export default {
        name: "past",
        filters:{
            parseTime:parseTime
        },
        props: ["elderlyCode","dictionary","disabled"],
        data(){
            return{
                dialogFormVisible:false,
                list:[],
                time:"",
                listLoading:true,
                diseases:"",
                diseasess:{

                },
                rules:{
                    isAcceptTreat: [{ required: true,trigger: "change", message: "是否接受治疗"}],
                    diagnosisTime: [{ required: true, trigger: "change", message: "确诊时间" ,}],
                    diseases:[{ required: true, trigger: "change", message: "疾病类型" ,}],


                },
                isAcceptTreats:[
                    {
                        value: 'false',
                        label: '否'
                    }, {
                        value: 'true',
                        label: '是'
                    },
                ],
                form:{
                    isAcceptTreat: "",
                    diagnosisTime:"",
                    diseases:"",
                    elderlyCode:"",
                }
            }

        },
        created(){

            for(var i=0;i<this.dictionary.length;i++){

                if(this.dictionary[i].mark=="diseases"){
                    this.diseasess=(this.dictionary[i].childs)

                }

            }

        },
        mounted(){
            this.search()
        },
        methods:{
            search(){

                console.log(this.elderlyCode)
                if(this.elderlyCode){
                    let elderlyCode=this.elderlyCode
                    search({elderlyCode}).then(res =>{
                        console.log(res)
                        this.list=res.data

                    })
                }else {

                }


            },
            handleadd(){
                this.form.diagnosisTime=""
                this.form.isAcceptTreat=""
                this.form.diseases=""
                this.dialogFormVisible=true
            },
            closeDialog(){
                this.$refs['form'].resetFields();
            },
            Saveadd(){
                this.form.elderlyCode=this.elderlyCode
                console.log(this.form)
                if(this.elderlyCode){
                    this.$refs["form"].validate(async (valid) => {
                        if (valid) {
                            const {code} = await add(this.form);
                            console.log(code)
                            if(code==200){
                                this.$message({
                                    message: "添加成功",
                                    type: 'success'
                                })
                                this.search()
                            }else {

                            }



                        } else {
                            return false;
                        }
                    });
                }else {
                    this.$message({
                        message: "请先添加基本信息",
                        type: 'warning'
                    })
                }


            },
            close(){
                this.dialogFormVisible=false
            },
            doDelete(index,row) {
                let elderlyCode=row.elderlyCode
                let diseases=row.diseases
                console.log(row)
                if (row.id) {
                    this.$baseConfirm("你确定要删除当前项吗", null, async () => {
                        const {code, msg } = await del(({elderlyCode,diseases}));
                        if(code==200){
                            this.$baseMessage("删除成功");
                            this.search()
                        }else {
                            this.$baseMessage("删除失败");
                        }
                    });
                }
            },
        },

    }
</script>

<style scoped>

</style>
